/**
 * 
 */
package com.luckmgr.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.luckmgr.entity.db.Activity;
import com.luckmgr.entity.db.ActivityLevel;
import com.luckmgr.entity.db.LuckUser;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.ActivityLevelService;
import com.luckmgr.service.ActivityService;
import com.luckmgr.service.LuckUserService;
import com.luckmgr.util.ExcelUtil;

/**
 * 
 * @author duanhx
 * @version [Ver 1.0; 2012-7-22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("/luckuser")
public class LuckUserController {
	
	@Autowired
	ActivityLevelService activityLevelServiceImpl;
	@Autowired
	ActivityService activityServiceImpl;
	
	@Autowired
	LuckUserService luckUserServiceImpl;
	
	@RequestMapping(value = "")
	public String main(Model model,HttpSession session,Integer aid,Integer lid,String name,Integer page) {
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("aid", aid);
		map.put("lid", lid);
		map.put("name", name);
		PageBean<LuckUser> pagebean = luckUserServiceImpl.selectListPage(map, page);
		List<Activity> activitys = activityServiceImpl.selectList(null);
		
		if(aid!=null){
			List<ActivityLevel> alevel = activityLevelServiceImpl.findLevelByAid(aid);
			model.addAttribute("alevel", alevel);
		}
		
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("activitys", activitys);
		model.addAttribute("aid", aid);
		model.addAttribute("lid", lid);
		model.addAttribute("name", name);
		return "luckuser/index";
	}
	
	
	
	
	
	@RequestMapping(value = "instart")
	public String instart(Model model,Integer id) {
		if(id==null){
			model.addAttribute("msg", "该用户已不存在！");
			return "luckuser/instart";
		}
		
		LuckUser prize = luckUserServiceImpl.selectByPrimaryKey(id);
		List<ActivityLevel> alevel = activityLevelServiceImpl.findLevelByAid(prize.getActivityId());
		model.addAttribute("entity", prize);
		model.addAttribute("alevel", alevel);
		
		return "luckuser/instart";
	}
	
	
	
	@RequestMapping(value = "uplevel")
	public String uplevel(Model model,HttpSession session,Integer id,Integer lid) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(null == id || null == lid){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "luckuser/instart";
		}
		
		LuckUser luckUser = luckUserServiceImpl.selectByPrimaryKey(id);
		if(null == luckUser){
			model.addAttribute("msg", "用户不存在或者已被其它人移除！");
			return "luckuser/instart";
		}
		
		Activity  activity= activityServiceImpl.selectByPrimaryKey(luckUser.getActivityId());
		if(null == activity || activity.getStatus()==1){
			model.addAttribute("msg", "活动开始或者已清除！");
			return "luckuser/instart";
		}
		
		ActivityLevel activityLevel = activityLevelServiceImpl.selectByPrimaryKey(lid);
		if(null ==activityLevel){
			model.addAttribute("msg", "所选活动不存在！");
			return "luckuser/instart";
		}
		luckUser.setLevelId(activityLevel.getId());
		luckUser.setLevelName(activityLevel.getLevelName());
		luckUser.setOperatorName(user.getUserName());
		luckUser.setOperatorIp(user.getLoginIp());
		
		luckUserServiceImpl.updateEntity(luckUser);
		return "redirect:/luckuser";
	}
	
	
	
	
	
	
	@RequestMapping(value = "addup")
	public String addup(Model model,Integer id) {
		LuckUser prize = null;
		if(null !=id){
			prize = luckUserServiceImpl.selectByPrimaryKey(id);
			model.addAttribute("entity", prize);
		}
		List<ActivityLevel> alevel = activityLevelServiceImpl.selectList(null);
		model.addAttribute("alevel", alevel);
		
		return "luckuser/addup";
	}
	
	
	

	
	
	@RequestMapping(value = "update")
	public String update(Model model,HttpSession session,Integer id,Integer lid) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(null == id || null == lid){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "luckuser/addup";
		}
		
		LuckUser luckUser = luckUserServiceImpl.selectByPrimaryKey(id);
		if(null == luckUser){
			model.addAttribute("msg", "用户不存在或者已被其它人移除！");
			return "luckuser/addup";
		}
		
		Activity  activity= activityServiceImpl.selectByPrimaryKey(luckUser.getActivityId());
		if(null == activity || activity.getStatus()==1){
			model.addAttribute("msg", "活动开始或者已清除！");
			return "luckuser/addup";
		}
		
		ActivityLevel activityLevel = activityLevelServiceImpl.selectByPrimaryKey(lid);
		if(null ==activityLevel){
			model.addAttribute("msg", "所选活动不存在！");
			return "luckuser/addup";
		}
		luckUser.setLevelId(activityLevel.getId());
		luckUser.setLevelName(activityLevel.getLevelName());
		luckUser.setOperatorName(user.getUserName());
		luckUser.setOperatorIp(user.getLoginIp());
		
		luckUserServiceImpl.updateEntity(luckUser);
		return "redirect:/luckuser";
	}
	
	


	@RequestMapping(value = "/tzupload")
	public String tzupload(HttpSession session,Integer aid, @RequestParam("file") MultipartFile file)
			throws IllegalStateException, IOException {
		try {
			User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
			Map<String, List<String[]>> map = ExcelUtil.findExcelMap(file);
			luckUserServiceImpl.betRecord(user,aid, map);
			//toolService.betRecord(activityid, map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		return "redirect:/luckuser";
	}
	

	@RequestMapping(value = "upfile")
	public String upfile(Model model,Integer id) {
		List<Activity> activitys = activityServiceImpl.selectList(null);
		model.addAttribute("activitys", activitys);
		return "luckuser/upfile";
	}


	
	@RequestMapping(value = "delete")
	public String comdelete(Model model,@RequestParam(required = false)Integer id) {
		if(null != id && 0 != id){
			luckUserServiceImpl.deleteByPrimaryKey(id);
		}
		return "redirect:/luckuser";
	}
	
	
	
	
	@RequestMapping(value = "indeluser")
	public String indeluser(Model model) {
		List<Activity> activitys = activityServiceImpl.selectList(null);
		model.addAttribute("activitys", activitys);
		return "luckuser/deluser";
	}

	
	
	@RequestMapping(value = "deluser")
	public String deluser(Model model,Integer aid) {
		if(null == aid || 0 == aid){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "luckuser/addup";
		}
		
		Activity  activity= activityServiceImpl.selectByPrimaryKey(aid);
		if(null == activity || activity.getStatus()==1){
			model.addAttribute("msg", "活动开始或者已清除！");
			return "luckuser/addup";
		}
		
		luckUserServiceImpl.deleteByActivity(aid);
		return "redirect:/luckuser";
	}
	
}
